/**
 * Copyright 2009 Takahiko Ito
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.unigram.oluolu.rqe.postprocessor;

import org.apache.hadoop.conf.Configuration;

/**
 * Implementation of IPostPorcessor for context dictionary creation.
 */
public class ContextPostProcessor
    extends NgramPostProcessor {
    
    /**
     * Constructor.
     * 
     * @param conf contains settings such as ratio threshold. 
     */
    public ContextPostProcessor(final Configuration conf) {
        super(conf);
        this.ratioThredhold = conf.getFloat(RATIO_THRESHOLD, 
                DEFAULT_RATIO_THRESHOLD);        
    }
    
    /**
     * Validate whether the related query pair is context relation. 
     * 
     * @param firstFreq frequency of first query string
     * @param secondFreq frequency of second query string
     * @param pairFreq frequency of co-occurrences of first and 
     *         second query strings in one session. 
     * @param totalFreq the number of queries in the input data
     * @return true when first and second query is context relation
     *          otherwise return false
     */
    @Override
    protected final boolean validate(final Long firstFreq,
            final Long secondFreq, final Long pairFreq, 
            final Long totalFreq) {
        if (secondFreq > (firstFreq * this.ratioThredhold)) {
            return true;
        } 
        return false;
        
    }
    
    /** symbol: likelihood threshold. */
    public static final String RATIO_THRESHOLD
        = "oluolu.rqe.levenshtein.threshold";    
    
    /** default: threshold for likelihood rate. */
    public static final float DEFAULT_RATIO_THRESHOLD = 0.01F;      
    
    /** threshold of log likelihood ratio. */
    private final float ratioThredhold;    
}
